<template>
    <div class="stats-panel">
        <div class="stats-panel-body">
            <div class="stats-btn-group">
                <button type="button" :class="{'stats-checked': chartType == 'userCount'}" @click="changeChartType('userCount')">{{$t('portal.dataBoard.userQuantity')}}</button>
                <button type="button" :class="{'stats-checked': chartType == 'loginCount'}" @click="changeChartType('loginCount')">{{$t('portal.dataBoard.loginNum')}}</button>
            </div>
            <div class="category-chart-wrap">
                <div id="pageviewsPie" ref="pageviewsPie" class="pie-item-container"></div>
            </div>
        </div>
    </div>
</template>

<script>
import echarts from 'echarts';
let currCharts = null;
export default {
    props: {
        data: {
            type: Object,
            default: () => ({})
        },
    },
    data() {
        return {
            chartType: 'userCount',
        }
    },
    watch: {
        data: function(newValue, oldValue){
            if (newValue) {
                this.updateCharts(newValue[this.chartType]);
            }
        },
    },
    mounted() {
        this.$nextTick(() => {
            this.initECharts()
        })
    },
    beforeDestroy() {
        currCharts = null;
    },
    methods: {
        changeChartType(type) {
            this.chartType = type
            this.updateCharts(this.data[type])
        },
        // 初始化图表
        initECharts() {
            currCharts = echarts.init(document.getElementById('pageviewsPie')); 
        },
        getEChartsOption(data) {
            const chartsData = data;
            const option = {
                    tooltip: {
                        trigger: 'item'
                    },
                    legend: {
                        type: 'scroll',
                        orient: 'horizontal',
                        bottom: 'bottom'
                    },
                    series: [
                        {
                        name: 'Access From',
                        type: 'pie',
                        radius: ['40%', '70%'],
                        center: ['50%', '40%'],
                        avoidLabelOverlap: false,
                        label: {
                            show: false,
                            position: 'center'
                        },
                        emphasis: {
                            label: {
                            show: true,
                            fontSize: 12,
                            fontWeight: 'bold'
                            }
                        },
                        labelLine: {
                            show: false
                        },
                        data: this.makeUpList(chartsData),
                    }]
                };
            return option;
        },
        getEchartsData(data) {
            return data || [];
        },
        updateCharts(update) {
            let data = this.getEchartsData(update);
            let option = this.getEChartsOption(data);
            currCharts.setOption(option);
        },
        makeUpList(data) {
            return data.map((item) => {
                return {
                    name: item.doorName,
                    value: item.userCount
                }
            })
        }
    },
};
</script>

<style lang="scss" scoped>
.stats-panel{
    margin-top: 0;
}
.stats-btn-group{
    margin-left: 16px;
    padding-top: 0;
}
.pie-item-container{
    height: calc(336px - 34px - 16px);
}
</style>
